﻿using Aliphant.Mvvm.XEvent.Events;
using Aliphant.Mvvm.XEvent.Events.Base.SubScription;
using Aliphant.UI.Wallpaper.Models;
using Aliphant.UI.Windows;
using System.Windows;
using System.Windows.Controls;

namespace Aliphant.UI.Wallpaper.Views
{
    /// <summary>
    /// WallpaperPage.xaml 的交互逻辑
    /// </summary>
    public partial class WallpaperPage : UserControl
    {
        private WindowEvent _windowEvent;

        SubScriptionToken? _subscriptionToken1;
        SubScriptionToken? _subscriptionToken2;

        public WallpaperPage()
        {
            InitializeComponent();

            _windowEvent = WallpaperUIModule.EventAgent.GetEvent<WindowEvent>();
            RegisterRegions();
            RegistSubScriptionToken();
        }

        private void RegisterRegions()
        {
            WallpaperUIModule.Container.RegisterRegion<ContentControl>(this.player, "player");
            WallpaperUIModule.Container.RegisterRegion<ContentControl>(this.preview, "preivewer");
            WallpaperUIModule.Container.RegisterRegion<ContentControl>(this.pictures, "pictures");
            WallpaperUIModule.Container.RegisterRegion<ContentControl>(this.toolbar, "toolbar");
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //WallpaperUIModule.Container.NavigateTo("player", "tablePager");
            WallpaperUIModule.Container.NavigateTo("preivewer", "preview");
            WallpaperUIModule.Container.NavigateTo("pictures", "pictureList");
            //WallpaperUIModule.Container.NavigateTo("toolbar", "toolsControl");
        }

        private void RegistSubScriptionToken()
        {
            _subscriptionToken1 = _windowEvent.Subscribe(OpenViewPictureWindow, (p) => p.WindowActionType == WindowActionType.Open && p.EventId == EventIds.OpenViewPictureWindow);
            _subscriptionToken2 = _windowEvent.Subscribe(OpenAddWindow, (p) => p.WindowActionType == WindowActionType.Open && p.EventId == EventIds.OpenAddWindow);
        }

        private void UnRegistSubScriptionToken()
        {
            _windowEvent.UnSubscribe(_subscriptionToken1);
            _windowEvent.UnSubscribe(_subscriptionToken2);
        }

        /// <summary>
        /// 打卡图片查看器
        /// </summary>
        /// <param name="args"></param>
        private void OpenViewPictureWindow(WindowEvent args)
        {
            WindowBase? window = new PicturePreviewWindow();
            window.Parameter = args.Data;
            window.Owner = Application.Current.MainWindow;
            window.OnShowDialog();
        }

        private void OpenAddWindow(WindowEvent args)
        {
            //WindowBase window = new SelectWindow();
            //window.Parameter = args.Data;
            //window.CallBack = args.CallBack;
            //window.Owner = this;
            //window.OnShowDialog();
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
